package com.chixing.util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;


/**
 * @author Wesley
 * @version 1.0
 * @date 2021/8/30 9:55
 */
public class MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory ;
    private static SqlSession sqlSession;
    static {
        InputStream is = MybatisUtil.class.getResourceAsStream("/com/chixing/config/mybatis-config.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    }
    public static SqlSession getSqlSession(){
        sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }

    public static void  closeSqlSession(){
        if(sqlSession !=null) {
            sqlSession.close();
        }
    }

}
